import { Component } from '@angular/core';
import { StringFieldSchemaSection } from '../../json-schema';
import { FormFieldBase } from '../form-field-base';

@Component({
  selector: 'app-string-form-field',
  templateUrl: './string-form-field.component.html',
  styleUrl: './string-form-field.component.scss'
})
export class StringFormFieldComponent extends FormFieldBase<StringFieldSchemaSection>{
  get controlBranch() {
    if (this.schema) {
      if (this.schema.enum?.length) {
        return "dropdown";
      }
      const { format, } = this.schema!;

      if (format) {
        switch (format) {
          case "color":
            return "color";
          case "date":
            return "date";
          case "time":
            return "time";
          case "email":
          case "tel":
          case "uri":
            return "text";
          default:
            return "text";
        }
      }
    }

    return "text";
  }

  get isDropdown() {
    if (this.schema?.enum?.length) {
      return true;
    } else {
      return false;
    }
  }
  get dropdownOptions() {
    return this.schema?.enum ?? [];
  }
  get inputType() {
    if (this.schema?.format) {
      const { format } = this.schema;
      if (format === "email") {
        return "email";
      }
      if (format === "uri") {
        return "url";
      }
      if (format === "tel") {
        return "tel";
      }
      if (format === "date") {
        return "date";
      }
      if (format === "time") {
        return "time";
      }
      if (this.schema.format === "date-time") {
        return "date-time";
      }
      // TODO: add more types
    }
    return "text";
  }
}
